<?php

/**
 * Classe prenant en charges les PDO Statement des requetes SQL select
 *
 * @category   McCube
 * @package    Db
 * @author     Leiha Sellier <leiha.sellier@gmail.com>
 * @license    http://creativecommons.org/licenses/by/3.0/  CC BY 3.0
 * @link       http://code.google.com/p/mechanics-cubes/
 * @see        http://www.php.net/manual/book.pdo.php
 */
class McCube_Db_Result{

    protected $_PDOStatement = null;
    protected $_selectObject = null;

    public function __construct($PDOStatement, $selectObject){
        $this->_PDOStatement = $PDOStatement;
        $this->_selectObject = $selectObject;
    }

    public function __call($method, $args){
        return call_user_func_array(array($this->_PDOStatement, $method), $args);
    }

    public function setFetchMode(){
        $this->__call('setFetchMode', func_get_args());
        return $this;
    }

    public function count(){
        return $this->_PDOStatement->rowCount();
    }

    public function fetch(){
        if(!$this->_PDOStatement->rowCount())
            return null;

        if($this->_selectObject->mustHaveSingleRow())
            return $this->_PDOStatement->fetch();

        return $this->_PDOStatement->fetchAll();
    }

    public function fetchArray(){
        if($result = $this->fetch())
            return $result;

        return array();
    }

}